<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/14 0014
 * Time: 14:17
 */
namespace app\admin\Controller;
use think\db;
use think\db\Where;
use app\admin\model\Yuyue as YuyueModel;
use think\Controller;
class Yuyue extends Controller{
    public function yuyueList(){
        return $this->fetch();
    }
    public function yuyueListData($page,$limit){
        $data=YuyueModel::with('driver','classtype')
            ->where('is_del',0)
            ->page($page,$limit)
            ->select();
        foreach ($data as $k=>$v){
            $data[$k]['jzname']=$v->driver->name;
            $data[$k]['km']=$v->classtype->class_name;
            $data[$k]['begin_end']='';
            if($v['typeid']==0) {
                $spec = Db::name('yuyue_spec')
                    ->where('yyid', $v['id'])
                    ->order('date', 'asc')
                    ->select();
                $data[$k]['begin_end'] = $spec[0]['date'] . '-----' . $spec[count($spec) - 1]['date'];
            }
        }
        $count=Db::name('yuyue')->count();
        return json(['code'=>0,'msg'=>'返回成功','count'=>$count,'data'=>$data]);
    }
    //添加预约数据
    public function yuyueAdd(){
        //获取后一天时间
        $data=date('Y-m-d',strtotime("+1 day"));
        //获取驾照类型
        $driver=Db::name('driver_type')
            ->select();
        //获取科目类型
        $kemu=Db::name('class_type')
            ->select();
        foreach ($kemu as $k=>$v){
            if(strstr($v['class_type'],'1')==false){
               unset($kemu[$k]);
            }
        }
        $this->assign('kemu',$kemu);
        $this->assign('driver',$driver);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function yueyueAddForm(){
        $post=input('post.');
        $data['typeid']=$post['data']['type'];
        $data['jzid']=$post['data']['driver'];
        $data['kmid']=$post['data']['kemu'];
        if($data['typeid']==1){
            $data['price']=$post['data']['price'];
            $data['senvenday']=$post['data']['senvenday'];
            $flag=Db::name('yuyue')->insert($data);
            if($flag){
                return json(['code'=>1,'msg'=>'添加数据成功']);
            }
            return json(['code'=>0,'msg'=>'添加数据失败']);
        }else{
            $flag=Db::name('yuyue')->insert($data);
            $yyid=Db::name('yuyue')->getLastInsID();
            $flag1=$this->setspec($yyid,$post);
            if(!$flag){
                return json(['code'=>1,'msg'=>'添加数据失败']);
            }
            if(!$flag1){
                return json(['code'=>1,'msg'=>'添加参数失败']);
            }
            return json(['code'=>1,'msg'=>'添加数据成功']);
        }
    }
    //获取教练
    public function getcoach(){
        $date=input('post.date');
        $coach=Db::name('coach')
            ->select();
        foreach ($coach as $k=>$v){
            if(strstr($v['qingjia'],$date)){
                $coach[$k]['status']=0;
            }else{
                $coach[$k]['status']=1;
            }
        }
        return json(['code'=>1,'msg'=>'success','data'=>$coach]);
    }
    //设置预约练车属性
    public function setspec($yyid,$post){
        $data=array();
        foreach ($post['date'] as $k=>$v){
            $data1['date']=$v;
            $data1['yyid']=$yyid;
            $data1['coachid']=rtrim($post['jiaolian'][$k]    ,',');
            $peizhi=rtrim($post['peizhi'][$k],'//');
            $arr=explode('//',$peizhi);
            $flag=Db::name('yuyue_spec')->insert($data1);
            if(!$flag){
                return false;
            }
            $dateid=Db::name('yuyue_spec')->getLastInsID();
            foreach ($arr as $kk=>$vv){
                $temps=explode('=',$vv);
                $data['time']=$temps[0];
                $data['price']=$temps[1];
                $data['count']=$temps[2];
                $data['dateid']=$dateid;
                $data['yyid']=$yyid;
                $flag=Db::name('yuyue_spec_detail')->insert($data);
                if(!$flag){
                    return false;
                }
            }
        }
        return true;
    }
    //编辑预约练车
    public function yuyueEdit(){
        $id=input('get.id');
        //获取日期，教练参数
        $spec=Db::name('yuyue_spec')
            ->where('yyid',$id)
            ->order('date','asc')
            ->select();
        //获取时间，库存价格参数
        $specdetail;
        foreach ($spec as $k=>$v){
            $spec_detail=Db::name('yuyue_spec_detail')
                ->where('dateid',$v['id'])
                ->order('id','asc')
                ->select();
            $spec[$k]['child']=$spec_detail;
            //去除请假的教练
            $coach=Db::name('coach')
                ->select();
            foreach ($coach as $kk=>$vv){
                if(strstr($vv['qingjia'],$v['date'])){
                    $coach[$kk]['status']=0;
                }else{
                    $coach[$kk]['status']=1;
                }
            }
            $spec[$k]['coach']=$coach;
        }
        //获取教练
        $coach=Db::name('coach')
            ->select();

        //获取驾照类型
        $driver=Db::name('driver_type')
            ->select();
        //获取科目类型
        $kemu=Db::name('class_type')
            ->select();
        foreach ($kemu as $k=>$v){
            if(strstr($v['class_type'],'1')==false){
                unset($kemu[$k]);
            }
        }
        //获取预约练车数据
        $data1=Db::name('yuyue')
            ->where('id',$id)
            ->find();
        $this->assign('data1',$data1);
        $this->assign('kemu',$kemu);
        $this->assign('driver',$driver);
        $this->assign('coach',$coach);
        $this->assign('data',$spec);
        return $this->fetch();
    }
    public function yuyueEditForm(){
        $post=input('post.');
        $data['typeid']=$post['data']['type'];
        $data['jzid']=$post['data']['driver'];
        $data['kmid']=$post['data']['kemu'];
        if($data['typeid']==1){
            $data['senvenday']=$post['data']['senvenday'];
            $data['price']=$post['data']['price'];
            $flag=Db::name('yuyue')
                ->where('id',$post['data']['id'])
                ->update($data);
            if($flag){
                return json(['code'=>1,'msg'=>'修改数据成功']);
            }
            return json(['code'=>0,'msg'=>'修改数据失败']);
        }else{
            $flag=Db::name('yuyue')
                ->where('id',$post['data']['id'])
                ->update($data);
            //删除原先的参数
            Db::name('yuyue_spec')
                ->where('yyid',$post['data']['id'])
                ->delete();
            Db::name('yuyue_spec_detail')
                ->where('yyid',$post['data']['id'])
                ->delete();
            //增加新有的参数
            $flag1=$this->setspec($post['data']['id'],$post);
            if($flag1){
                return json(['code'=>1,'msg'=>'修改数据成功']);
            }
            if(!$flag1){
                return json(['code'=>-2,'msg'=>'修改参数失败']);
            }
            return json(['code'=>1,'msg'=>'修改数据成功']);
        }
    }
    //删除
    public function del(){
        $post=input('post.');
        $data['is_del']=1;
        $flag=Db::name('yuyue')
            ->where('id','in',$post['ids'])
            ->update($data);
        if($flag){
            return json(['code'=>1,'msg'=>'删除成功']);
        }
        return json(['code'=>0,'msg'=>'删除失败']);
    }

}
